package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.entity.Student;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.StudentService;
import cn.ztydata.teachingsystem.heaven.web.dto.ClassGroup.StudentInfoDto;
import cn.ztydata.teachingsystem.heaven.web.dto.StudentDto;
import cn.ztydata.teachingsystem.heaven.web.dto.UserDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.util.StringCodeUtil;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.UUID;

/**
 * 学生控制器
 * Created by wyh on 2015/3/21.
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;
    @Value("${session.key}")
    private String sessionKey;

    //<editor-fold "课程群组">

    /**
     * 学生列表
     *
     * @param depart   系部
     * @param major    专业
     * @param search   名字
     * @param page     页数
     * @param pageSize 条数
     * @return JsonResultPage
     * @author wyh
     * @since 2015-03-23
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage<StudentDto> listStudents(@RequestParam(value = "depart", required = false) String depart,
                                                   @RequestParam("major") String major,
                                                   @RequestParam(value = "search", required = false) String search,
                                                   @RequestParam("page") int page,
                                                   @RequestParam("pageSize") int pageSize) {
        //校验
        Assert.isTrue(page > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //中文转码
        search = StringCodeUtil.urlParamTransCode(search, "utf-8");

        Page<Student> studentPage = studentService.listStudents(depart, major, search, page, pageSize);

        log.debug("返回 学生列表");
        return new JsonResultPage<>(new Page<>(studentPage.getTotal(), studentPage.getSize(), studentPage.getNum(), mapper.mapList(studentPage.getItems(), StudentDto.class)));
    }

    /**
     * 学生详细
     *
     * @param id 学生ID
     * @return JsonResultData
     * @author wyh
     * @since 2015-03-23
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData<StudentDto> getDetail(@RequestParam("id") long id) {
        //校验参数
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "学生ID"));

        log.debug("返回 学生详细");
        return new JsonResultData<>(mapper.map(studentService.getDetail(id), StudentDto.class));
    }

    /**
     * 重置密码
     *
     * @param id 学生ID
     * @return Json
     * @author wyh
     * @since 2015-03-23
     */
    @RequestMapping(value = "/resetPassword/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult resetPwd(@PathVariable long id) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "学生ID"));

        studentService.changePwd(id, "123456");
        log.debug("返回 重置密码成功");
        return new JsonResult(true, "重置成功");
    }


    /**
     * 分页获取课程群组同学列表
     *
     * @param courseCode 课程代码
     * @param pageNum    当前页码
     * @param pageSize   每页条数
     * @param letter     姓名首字母
     * @param name       姓名
     * @return JsonResultList
     * @author lyj
     * @since 2015-4-1
     */
    @RequestMapping(value = "/lessonGroupList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getGroupList(@RequestParam("lessonCode") String courseCode,
                                       @RequestParam("page") int pageNum,
                                       @RequestParam("pageSize") int pageSize,
                                       @RequestParam(value = "letter", required = false, defaultValue = "0") String letter,
                                       @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        Assert.notNull(courseCode, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程代码"));
        Assert.hasLength(courseCode, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程代码"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //汉字转码
        courseCode = StringCodeUtil.urlParamTransCode(courseCode, "utf-8");
        letter = StringCodeUtil.urlParamTransCode(letter, "utf-8");
        name = StringCodeUtil.urlParamTransCode(name, "utf-8");

        Page<Student> page = studentService.getGroupList(courseCode, pageNum, pageSize, letter, name);

        log.debug("分页获取课程群组同学列表");
        return new JsonResultPage(new Page<>(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), StudentDto.class)));
    }


    /**
     * 获取课程群组活跃学生列表
     *
     * @param courseCode 课程代码
     * @param count 条数
     * @return JsonResultList
     * @author lyj
     * @since 2015-4-1
     */
    @RequestMapping(value = "/active", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList getHotStudent(@RequestParam("lessonCode") String courseCode,
                                        @RequestParam("pageSize") int count,
                                        @RequestParam("groupId") long groupId){
        Assert.notNull(courseCode, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程代码"));
        Assert.hasLength(courseCode, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "课程代码"));
        Assert.isTrue(count > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "条数"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));

        log.debug("获取活跃学生列表");
        return new JsonResultList(mapper.mapList(studentService.getHotStudent(courseCode, count, groupId), Student.class));
    }

    //</editor-fold>


    //<editor-fold "班级群组">

    /**
     * 获取班级群组活跃学生列表
     *
     * @param classNum 班级代码
     * @param classId 班级ID
     * @param count
     * @return JsonResultList
     * @author lyj
     * @since 2015-5-5
     */
    @RequestMapping(value = "/classActive", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList getHotClassStudent(@RequestParam("code") String classNum,
                                             @RequestParam("id") long classId,
                                             @RequestParam("pageSize") int count,
                                             HttpServletRequest request) {
        Assert.notNull(classNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级代码"));
        Assert.hasLength(classNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级代码"));
        Assert.isTrue(classId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级ID"));
        Assert.isTrue(count > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "条数"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        log.debug("获取班级群组活跃学生列表");
        return new JsonResultList(mapper.mapList(studentService.getHotClassStudent(classNum, classId, count, userInfo), StudentInfoDto.class));
    }


    /**
     * 根据班级代码分页获取班级同学列表
     *
     * @param classNum 班级代码
     * @param letter   姓名首字母
     * @param pageNum  当前页码
     * @param pageSize 每页条数
     * @param search   关键字
     * @return JsonResultPage
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/classmates", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getSchoolMate(@RequestParam("code") String classNum,
                                     @RequestParam(value = "letter", required = false, defaultValue = "0") String letter,
                                     @RequestParam("page") int pageNum,
                                     @RequestParam("pageSize") int pageSize,
                                     @RequestParam(value = "search", required = false, defaultValue = "") String search,
                                     @RequestParam("id") long classId,
                                     HttpServletRequest request) {

        Assert.notNull(classNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级代码"));
        Assert.hasLength(classNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级代码"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(classId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "班级ID"));

        //汉字转码
        classNum = StringCodeUtil.urlParamTransCode(classNum, "utf-8");
        letter = StringCodeUtil.urlParamTransCode(letter, "utf-8");
        search = StringCodeUtil.urlParamTransCode(search, "utf-8");

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Student> page = studentService.getSchoolMate(classId,classNum, letter, pageNum, pageSize, search, userInfo);

        log.debug("1.根据班级代码分页获取班级同学列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), StudentInfoDto.class)));

    }

    //</editor-fold>

    /**
     * 学生验证  用于游戏方调用接口  取学生信息
     * @param dto
     * @param request
     * @return JsonResult
     *
     * @author hesufang
     * @since 2016-04-19
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getStudentMsg(@RequestBody UserDto dto, HttpServletRequest request){

        //dto转为entity
        User user = mapper.map(dto, User.class);

        //判断用户登录信息
        Student student = studentService.checkUserLogin(user);

        //判断是否获取用户信息
        if(null == student){
            log.debug("studentInfo is not exist:null==student");
            return new JsonResult(false, "用户名或密码错误");
        }
        StudentDto studentDto = mapper.map(student, StudentDto.class);
        log.debug("studentInfo is exist:null!=student");
        return new JsonResultData<>(studentDto);
    }
}
